package rdd.operate;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import scala.Int;
import scala.Tuple2;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class Spark67_Operate_CountByKey {
    public static void main(String[] args) {
        final SparkConf conf = new SparkConf();
        conf.setMaster("local");
        conf.setAppName("spark");
        final JavaSparkContext jsc = new JavaSparkContext(conf);

        final List<Integer> nums = Arrays.asList(4,3,2,1);
        final JavaRDD<Integer> rdd = jsc.parallelize(nums,2);

        JavaPairRDD<Integer, Integer> pairRDD = rdd.mapToPair(
                num -> new Tuple2<>(num, num)
        );

        //需要在KV类型的基础上进行
        final Map<Integer,Long> map = pairRDD.countByKey();
        System.out.println(map);

        jsc.close();
    }
}
